package com.example.blog.config;

import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Configuration
@EnableCaching
public class CacheConfig {

    @Bean
    public Caffeine<Object, Object> caffeineConfig() {
        return Caffeine.newBuilder()
            .expireAfterWrite(1, TimeUnit.HOURS)  // 写入1小时后过期
            .expireAfterAccess(30, TimeUnit.MINUTES)  // 30分钟未访问后过期
            .initialCapacity(100)  // 初始容量
            .maximumSize(1000)  // 最大容量
            .recordStats();  // 开启统计
    }

    @Bean
    public CacheManager cacheManager(Caffeine<Object, Object> caffeine) {
        CaffeineCacheManager cacheManager = new CaffeineCacheManager();
        cacheManager.setCaffeine(caffeine);
        cacheManager.setCacheNames(java.util.Arrays.asList("dashboard"));
        return cacheManager;
    }
} 